package com.dennytech.tac.base;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.dennytech.tac.statistics.Stat;
import com.dennytech.tac.ui.activity.MainActivityV2;
import com.dennytech.tac.widget.SearchBarEditText;

public abstract class CFActivity extends SherlockFragmentActivity {

	protected boolean isDestoryed;

	protected TextView titleText;
	protected TextView subtitleText;
	protected ImageButton leftTitleButton;
	protected TextView leftTitleTextButton;
	protected ImageButton rightTitleButton;
	protected Button titleButton;
	protected SearchBarEditText searchEdt;

	@Override
	protected void onCreate(Bundle b) {
		super.onCreate(b);
		if (!Environment.isDebug()) {
			Stat.pageView(getClass().getName());
		}

		if (!(this instanceof MainActivityV2)) {
			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}
		// initCustomTitle();
		initView();
	}

	public boolean onOptionsItemSelected(
			com.actionbarsherlock.view.MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			if (!(this instanceof MainActivityV2)) {
				finish();
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	};

	/**
	 * you must setContentView at first
	 */
	protected abstract void initView();

	@Override
	protected void onDestroy() {
		isDestoryed = true;
		super.onDestroy();
	}

	Dialog mDialog;

	public void showProgressDialog(String message,
			OnCancelListener cancleListener) {
		if (isDestoryed)
			return;
		dismissDialog();

		ProgressDialog dlg = new ProgressDialog(this);
		dlg.setOnCancelListener(cancleListener);
		dlg.setOnKeyListener(new DialogInterface.OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_SEARCH)
					return true;
				return false;
			}
		});
		dlg.setMessage(message);

		mDialog = dlg;
		dlg.show();
	}

	public void dismissDialog() {
		if (isDestoryed)
			return;
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.dismiss();
			mDialog = null;
		}
	}

	public Dialog createDialog(int menuResId,
			android.content.DialogInterface.OnClickListener l) {
		Dialog dialog = null;
		Builder builder = new android.app.AlertDialog.Builder(this);
		builder.setItems(menuResId, l);
		dialog = builder.create();
		return dialog;
	}

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		setBackground();
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		super.setContentView(view, params);
		setBackground();
	}

	@Override
	public void setContentView(View view) {
		super.setContentView(view);
		setBackground();
	}

	protected void setBackground() {
		FrameLayout rootFrame = (FrameLayout) findViewById(android.R.id.content);
		rootFrame.setBackgroundColor(Color.WHITE);
	}

}
